package com.wootion.cims.module.basicinformation.plotsetting.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wootion.cims.dao.*;
import com.wootion.cims.general.utils.GetIp;
import com.wootion.cims.module.basicinformation.plotsetting.dto.PlotDto;
import com.wootion.cims.module.basicinformation.plotsetting.model.Building;
import com.wootion.cims.module.basicinformation.plotsetting.model.BungalowVilla;
import com.wootion.cims.module.basicinformation.plotsetting.model.PlotMessage;
import com.wootion.cims.module.basicinformation.plotsetting.model.Unit;
import com.wootion.cims.module.basicinformation.plotsetting.service.PlotSettingService;
import com.wootion.cims.module.communityresidence.residencelist.model.Residence;
import com.wootion.cims.module.systemsetting.log.model.SysLog;
import com.wootion.cims.module.systemsetting.polt.model.SysPlot;
import com.wootion.cims.module.systemsetting.user.model.SysUserRole;
import com.wootion.cims.module.systemsetting.user.model.User;
import com.wootion.cims.response.ResponseResult;
import com.wootion.cims.response.ResultCode;
import com.wootion.cims.shiro.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: jack
 * @Date: 2022/12/02/14:51
 * @Description:
 */
@Service
@Slf4j
public class PlotSettingServiceImpl implements PlotSettingService {

    @Resource
    private SysRoleDao sysRoleDao;

    @Resource
    private SysLogDao sysLogDao;

    @Resource
    private SysPlotDao sysPlotDao;

    @Resource
    private SysUserRoleDao sysUserRoleDao;
    
    @Resource
    private ResidenceDao residenceDao;

    /**
     * 根据条件查询小区信息
     *
     * @param condition
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ResponseResult list(String condition, Integer page, Integer limit) {
        //获取当前登陆用户所能查看的所有社区下的所有小区
        User user = (User) ShiroUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper5.eq(SysUserRole::getUserId, user.getId());
        SysUserRole sysUserRole = sysUserRoleDao.selectOne(lambdaQueryWrapper5);
        String roleKey = sysRoleDao.findByRoles(sysUserRole.getRoleId());
        if ("street:admin".equals(roleKey)) {
            PageDTO<PlotDto> sysPlotPage = new PageDTO<>(page, limit);
            PageDTO<PlotDto> byPageDto = sysPlotDao.findByPageDto(sysPlotPage, condition);
            return ResponseResult.builder().data(byPageDto).build();
        }
        List<SysPlot> sysPlots = sysPlotDao.findUserIdBySysPlot(user.getId());
        if(sysPlots == null || sysPlots.size()<=0){
            return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message("当前用户还没有任何社区权限").build();
        }
        Set<String> sysPlotIds = new HashSet<>();
        for (SysPlot sysPlot : sysPlots) {
            sysPlotIds.add(sysPlot.getId());
        }
        List<String> ids = new ArrayList<>(sysPlotIds);
        PageDTO<PlotDto> pageDTO = new PageDTO<>(page,limit);
        PageDTO<PlotDto> pagePlotDto = sysPlotDao.findPagePlotDto(pageDTO, condition, ids);
        return ResponseResult.builder().data(pagePlotDto).build();
    }
    /**
     * 根据小区id查询小区的楼栋信息，单元信息，和平房/别墅信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult find(String id) {
        if(!StringUtils.hasText(id)){
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId,id);
        SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
        PlotMessage plotMessage2 = new PlotMessage();
        if(sysPlot.getBuildingUnit() != null){
            String buildingUnit = sysPlot.getBuildingUnit();
            PlotMessage plotMessage = JSON.parseObject(buildingUnit, PlotMessage.class);
            plotMessage2.setId(sysPlot.getId());
            plotMessage2.setBuildings(plotMessage.getBuildings());
        }
        if(sysPlot.getBungalowVilla() != null){
            String bungalowVilla = sysPlot.getBungalowVilla();
            PlotMessage plotMessage1 = JSON.parseObject(bungalowVilla, PlotMessage.class);
            plotMessage2.setBungalowVillas(plotMessage1.getBungalowVillas());
        }
        return ResponseResult.builder().data(plotMessage2).build();
    }

    /**
     * 添加楼栋
     * @param plotMessage
     * @return
     */
    @Override
    public ResponseResult saveBuilding(PlotMessage plotMessage) {
        Building building2 = plotMessage.getBuildings().get(0);
        String storiedBuilding = building2.getStoriedBuilding();
        if(StringUtils.hasText(storiedBuilding) && storiedBuilding.contains("/")){
            return ResponseResult.builder().message("楼房号不能有特殊字符，请跟换楼号").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        //没有传id是新增 {"id":"1","buildings": [{"storiedBuilding": "2"}]}
        if (Objects.isNull(building2.getId())) {
            String building = plotMessage.getBuildings().get(0).getStoriedBuilding();
            if (!StringUtils.hasText(building)) {
                return ResponseResult.builder().message("楼房号不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
            }
            if (!StringUtils.hasText(plotMessage.getId())) {
                return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
            }
            LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SysPlot::getId, plotMessage.getId());
            SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
            String buildingUnit = sysPlot.getBuildingUnit();

            //一个栋栋都没有的时候新增
            if (!StringUtils.hasText(buildingUnit)) {
                List<Building> buildingList = new ArrayList<>();
                Building building1 = plotMessage.getBuildings().get(0);
                building1.setId(IdUtil.simpleUUID());
                buildingList.add(building1);
                plotMessage.setBuildings(buildingList);
                return ResponseResult.builder().data(plotMessage).build();
            }
            //如果已经有了一些楼栋，则去判重
            PlotMessage plotMessage2 = JSON.parseObject(buildingUnit, PlotMessage.class);
            List<Building> buildingList = plotMessage2.getBuildings();
            for (Building building1 : buildingList) {
                if (plotMessage.getBuildings().get(0).getStoriedBuilding().equals(building1.getStoriedBuilding())) {
                    return ResponseResult.builder().message("当前楼号已经存在，请更换楼号！").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
                }
            }
            plotMessage.getBuildings().get(0).setId(IdUtil.simpleUUID());
            buildingList.add(plotMessage.getBuildings().get(0));
            plotMessage2.setBuildings(buildingList);
            return ResponseResult.builder().data(plotMessage2).build();
        }
        return ResponseResult.builder().build();
    }
    /**
     * 修改楼栋
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateBuilding(PlotMessage plotMessage) {
        Building building2 = plotMessage.getBuildings().get(0);
        String storiedBuilding = building2.getStoriedBuilding();
        if(StringUtils.hasText(storiedBuilding) && storiedBuilding.contains("/")){
            return ResponseResult.builder().message("楼房号不能有特殊字符，请跟换楼号").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        //如果传来了id,则是去修改 {"id":"1","buildings": [{"id":"2","storiedBuilding": "2"}]}
        String building = plotMessage.getBuildings().get(0).getStoriedBuilding();
        String buildingId = plotMessage.getBuildings().get(0).getId();//楼栋id
        if (!StringUtils.hasText(building)) {
            return ResponseResult.builder().message("楼房号不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        if (!StringUtils.hasText(plotMessage.getId())) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        if (!StringUtils.hasText(buildingId)) {
            return ResponseResult.builder().message("楼栋id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId, plotMessage.getId());
        SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
        String buildingUnit = sysPlot.getBuildingUnit();
        PlotMessage message = JSON.parseObject(buildingUnit, PlotMessage.class);
        List<Building> buildings = message.getBuildings();
        for (Building building1 : buildings) {
            if ((!building1.getId().equals(plotMessage.getBuildings().get(0).getId())) && building1.getStoriedBuilding().equals(plotMessage.getBuildings().get(0).getStoriedBuilding())) {
                return ResponseResult.builder().message("当前楼号已经存在，请更换楼号！").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
            }
        }
        return ResponseResult.builder().build();
    }
    /**
     * 删除楼栋
     *
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult deleteBuilding(PlotMessage plotMessage) {
        String id = plotMessage.getId();
        if (!StringUtils.hasText(id)) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String id1 = plotMessage.getBuildings().get(0).getId();
        if (!StringUtils.hasText(id1)) {
            return ResponseResult.builder().message("楼号id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId, plotMessage.getId());
        SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
        String buildingUnit = sysPlot.getBuildingUnit();
        PlotMessage plotMessage1 = JSON.parseObject(buildingUnit, PlotMessage.class);
        List<Building> buildings = plotMessage1.getBuildings();
        for (int i = 0; i < buildings.size(); i++) {
            if (buildings.get(i).getId().equals(plotMessage.getBuildings().get(0).getId())) {
                List<Unit> units = buildings.get(i).getUnits();
                if ((!Objects.isNull(units)) && units.size() > 0) {
                    return ResponseResult.builder().message("当前楼有关联的单元，不能删除").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
                }
            }
        }
        return ResponseResult.builder().build();
    }

    /**
     * 新增单元
     *
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveUnit(PlotMessage plotMessage) {
        plotMessage.getBuildings().get(0).getUnits().get(0).setId(IdUtil.simpleUUID());
        return  ResponseResult.builder().data(plotMessage).build();
    }
    /**
     * 修改单元
     *
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateUnit(PlotMessage plotMessage) {
        String id1 = plotMessage.getBuildings().get(0).getId();
        if (!StringUtils.hasText(id1)) {
            return ResponseResult.builder().message("楼号id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String id = plotMessage.getId();
        if (!StringUtils.hasText(id)) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String unit1 = plotMessage.getBuildings().get(0).getUnits().get(0).getUnit();
        if(StringUtils.hasText(unit1) && unit1.contains("/")){
            return ResponseResult.builder().message("单元号不能有特殊字符，请跟换单元号").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId, plotMessage.getId());
        SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
        String buildingUnit = sysPlot.getBuildingUnit();
        PlotMessage plotMessage2 = JSON.parseObject(buildingUnit, PlotMessage.class);
        List<Building> buildings = plotMessage2.getBuildings();
        //{"buildings": [{"id": "1", "units": [{"id": 1, "unit": "一单元"}, {"id": 2, "unit": "二单元"}], "storiedBuilding": "1"}]}
        //如果单元传了id,则代表修改单元
        for (Building building1 : buildings) {
            if (building1.getId().equals(plotMessage.getBuildings().get(0).getId())) {
                List<Unit> units = building1.getUnits();
                for (Unit unit : units) {
                    if ((!unit.getId().equals(plotMessage.getBuildings().get(0).getUnits().get(0).getId())) && unit.getUnit().equals(plotMessage.getBuildings().get(0).getUnits().get(0).getUnit())) {
                        return ResponseResult.builder().message("当前楼号中的单元已经存在，请更换单元名称以后再修改！").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
                    }
                }
                return ResponseResult.builder().build();
            }
        }
        return ResponseResult.builder().build();
    }
    /**
     * 删除单元
     *
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult deleteUnit(PlotMessage plotMessage) {

        String id2 = plotMessage.getBuildings().get(0).getId();
        if (!StringUtils.hasText(id2)) {
            return ResponseResult.builder().message("楼号id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String id = plotMessage.getId();
        if (!StringUtils.hasText(id)) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String id1 = plotMessage.getBuildings().get(0).getUnits().get(0).getId();
        if (!StringUtils.hasText(id1)) {
            return ResponseResult.builder().message("单元id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId, id);
        SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
        String buildingUnit = sysPlot.getBuildingUnit();
        PlotMessage plotMessage2 = JSON.parseObject(buildingUnit, PlotMessage.class);
        List<Building> buildings = plotMessage2.getBuildings();
        String storiedBuildingName = null;
        String unitName = null;
        for (Building building1 : buildings) {
            //找到对应的楼号
            if (building1.getId().equals(id2)) {
                storiedBuildingName = building1.getStoriedBuilding();
                List<Unit> units = building1.getUnits();
                for (int i = 0; i < units.size(); i++) {
                    //找到对应的单元
                    if (units.get(i).getId().equals(id1)) {
                        unitName = units.get(i).getUnit();
                        String address = storiedBuildingName + '/' + unitName;
                        LambdaQueryWrapper<Residence> wrapper = new LambdaQueryWrapper<>();
                        wrapper.eq(Residence::getAddress,address);
                        wrapper.eq(Residence::getPlotId,id);
                        wrapper.eq(Residence::getEnabled,true);
                        List<Residence> residences = residenceDao.selectList(wrapper);
                        if(!Objects.isNull(residences) && residences.size()>0){
                            return ResponseResult.builder().message("当前单元下，关联的有住宅不能删除").code(ResultCode.SYSTEM_ERROR.getCode()).build();
                        }
                    }
                }
                return ResponseResult.builder().build();
            }
        }
        return ResponseResult.builder().build();
    }
    /**
     * 添加平房/别墅区域
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveVilla(PlotMessage plotMessage) {
        String id = plotMessage.getId();
        if (Objects.isNull(id)) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        BungalowVilla bungalowVilla = plotMessage.getBungalowVillas().get(0);
        if(Objects.isNull(bungalowVilla)){
            return ResponseResult.builder().message("平房别墅信息不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String id1 = plotMessage.getBungalowVillas().get(0).getId();
        String area = plotMessage.getBungalowVillas().get(0).getArea();
        if(StringUtils.hasText(area) && area.contains("/")){
            return ResponseResult.builder().message("区域名称不能有特殊字符，请跟换区域名称").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        //如果没有传入平房/别墅id，那么则是新增
        if(!StringUtils.hasText(id1)){
            LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SysPlot::getId, id);
            SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
            String bungalowVilla1 = sysPlot.getBungalowVilla();
            //如果当前还未添加任何平房和别墅
            if(!StringUtils.hasText(bungalowVilla1)){
                plotMessage.getBungalowVillas().get(0).setId(IdUtil.simpleUUID());
                return ResponseResult.builder().data(plotMessage).build();
            }else{
                //如果已经添加了一些平房和别墅，再继续新增
                PlotMessage plotMessage1 = JSON.parseObject(bungalowVilla1, PlotMessage.class);
                List<BungalowVilla> bungalowVillas = plotMessage1.getBungalowVillas();
                for (BungalowVilla villa : bungalowVillas) {
                    if(villa.getArea().equals(bungalowVilla.getArea())){
                        return ResponseResult.builder().message("当前添加的平房/别墅区域已经重复了，请更换名称之后，再添加").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
                    }
                }
                bungalowVilla.setId(IdUtil.simpleUUID());
                plotMessage1.getBungalowVillas().add(bungalowVilla);
                return ResponseResult.builder().data(plotMessage1).build();
            }
        }
        return ResponseResult.builder().code(ResultCode.PARAM_IS_INVALID.getCode()).message(ResultCode.PARAM_IS_INVALID.getMessage()).build();
    }
    /**
     * 修改平房/别墅区域
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateVilla(PlotMessage plotMessage) {
        String id = plotMessage.getId();
        if (Objects.isNull(id)) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        BungalowVilla bungalowVilla = plotMessage.getBungalowVillas().get(0);
        if(Objects.isNull(bungalowVilla)){
            return ResponseResult.builder().message("平房别墅信息不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String id1 = plotMessage.getBungalowVillas().get(0).getId();
        String area = plotMessage.getBungalowVillas().get(0).getArea();
        if(StringUtils.hasText(area) && area.contains("/")){
            return ResponseResult.builder().message("区域名称不能有特殊字符，请跟换区域名称").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        //如果传了平房/别墅id，那么则是修改
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId, id);
        SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
        String bungalowVilla1 = sysPlot.getBungalowVilla();
        PlotMessage plotMessage1 = JSON.parseObject(bungalowVilla1, PlotMessage.class);
        List<BungalowVilla> bungalowVillas = plotMessage1.getBungalowVillas();
        for (BungalowVilla villa : bungalowVillas) {
            if(villa.getArea().equals(bungalowVilla.getArea()) && (!id1.equals(villa.getId()))){
                return ResponseResult.builder().message("当前添加的平房/别墅区域已经重复了，请更换名称之后，再修改").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
            }
        }
        return  ResponseResult.builder().data(plotMessage1).build();
    }
    /**
     * 删除平房、别墅区域
     * @param plotMessage
     * @return
     */
    @Override
    @Transactional
    public ResponseResult deleteVilla(PlotMessage plotMessage) {
        String id = plotMessage.getId();
        if (!StringUtils.hasText(id)) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        String id1 = plotMessage.getBungalowVillas().get(0).getId();
        if (Objects.isNull(id1)) {
            return ResponseResult.builder().message("平房/别墅区域的id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId, id);
        SysPlot sysPlot = sysPlotDao.selectOne(lambdaQueryWrapper);
        String bungalowVilla1 = sysPlot.getBungalowVilla();
        PlotMessage plotMessage1 = JSON.parseObject(bungalowVilla1, PlotMessage.class);
        List<BungalowVilla> bungalowVillas = plotMessage1.getBungalowVillas();
        for (int i = 0; i < bungalowVillas.size(); i++) {
            if(bungalowVillas.get(i).getId().equals(id1)){
                String area = bungalowVillas.get(i).getArea();
                LambdaQueryWrapper<Residence> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Residence::getAddress,area);
                wrapper.eq(Residence::getPlotId,id);
                wrapper.eq(Residence::getEnabled,true);
                List<Residence> residenceList = residenceDao.selectList(wrapper);
                if(!Objects.isNull(residenceList) && residenceList.size()>0){
                    return ResponseResult.builder().message("当前区域下，关联的有住宅不能删除").code(ResultCode.SYSTEM_ERROR.getCode()).build();
                }
            }
        }
        return  ResponseResult.builder().build();
    }
    /**
     * 更新保存用户修改之后的小区信息
     * @param sysPlot
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateSave(SysPlot sysPlot, HttpServletRequest request) {
        String id = sysPlot.getId();
        if (!StringUtils.hasText(id)) {
            return ResponseResult.builder().message("小区id不能为空").code(ResultCode.PARAM_IS_INVALID.getCode()).build();
        }
        SysLog sysLog = new SysLog();
        LambdaQueryWrapper<SysPlot> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlot::getId,id);
        SysPlot sysPlot1 = sysPlotDao.selectOne(lambdaQueryWrapper);
        StringBuilder builder = new StringBuilder();
        if(sysPlot1.getHouseholdsAvailable() != null){
            if(!sysPlot1.getHouseholdsAvailable().equals(sysPlot.getHouseholdsAvailable())){
                builder.append("应住户数,修改前:").append(sysPlot1.getHouseholdsAvailable()).append(",修改后:").append(sysPlot.getHouseholdsAvailable()).append(".");
            }
        }else{
            if(sysPlot.getHouseholdsAvailable() != null){
                builder.append("应住户数,修改前:0").append(",修改后:").append(sysPlot.getHouseholdsAvailable().intValue()).append(".");
            }
        }
        if(StringUtils.hasText(sysPlot1.getPropertyName())){
            if(!sysPlot1.getPropertyName().equals(sysPlot.getPropertyName())){
                builder.append("物业名称,修改前:").append(sysPlot1.getPropertyName()).append(",修改后:").append(sysPlot.getPropertyName()).append(".");
            }
        }else{
            if(StringUtils.hasText(sysPlot.getPropertyName())){
                builder.append("物业名称,修改前:无").append(",修改后:").append(sysPlot.getPropertyName()).append(".");
            }
        }
        if(StringUtils.hasText(sysPlot1.getPropertyManager())){
            if(!sysPlot1.getPropertyManager().equals(sysPlot.getPropertyManager())){
                builder.append("物业经理,修改前:").append(sysPlot1.getPropertyManager()).append(",修改后:").append(sysPlot.getPropertyManager()).append(".");
            }
        }else {
            if(StringUtils.hasText(sysPlot.getPropertyManager())){
                builder.append("物业经理,修改前:无").append(",修改后:").append(sysPlot.getPropertyManager()).append(".");
            }
        }
        if(StringUtils.hasText(sysPlot1.getPropertyPhone())){
            if(!sysPlot1.getPropertyPhone().equals(sysPlot.getPropertyPhone())){
                builder.append("物业联系电话,修改前:").append(sysPlot1.getPropertyPhone()).append(",修改后:").append(sysPlot.getPropertyPhone()).append(".");
            }
        }else {
            if(StringUtils.hasText(sysPlot.getPropertyPhone())){
                builder.append("物业联系电话,修改前:无").append(",修改后:").append(sysPlot.getPropertyPhone()).append(".");
            }
        }
        String buildingUnit = sysPlot1.getBuildingUnit();//楼房
        if(StringUtils.hasText(buildingUnit)){
            PlotMessage plotMessage = JSON.parseObject(buildingUnit, PlotMessage.class);
            builder.append("楼栋修改前:");
            for (Building building : plotMessage.getBuildings()) {
                builder.append("楼号:").append(building.getStoriedBuilding()).append(",").append(building.getStoriedBuilding()).append("下的单元有:");
                List<Unit> units = building.getUnits();
                for (Unit unit : units) {
                    builder.append(unit.getUnit());
                    if(units.get(units.size()-1).equals(unit)){
                        builder.append(".");
                    }else {
                        builder.append(",");
                    }
                }
            }
        }
        String buildingUnit1 = sysPlot.getBuildingUnit();
        if(StringUtils.hasText(buildingUnit1)){
            PlotMessage plotMessage = JSON.parseObject(buildingUnit1, PlotMessage.class);
            builder.append("楼栋修改后:");
            for (Building building : plotMessage.getBuildings()) {
                builder.append("楼号:").append(building.getStoriedBuilding()).append(",").append(building.getStoriedBuilding()).append("下的单元有:");
                List<Unit> units = building.getUnits();
                for (Unit unit : units) {
                    builder.append(unit.getUnit());
                    if(units.get(units.size()-1).equals(unit)){
                        builder.append(".");
                    }else {
                        builder.append(",");
                    }
                }
            }
        }
        String bungalowVilla = sysPlot1.getBungalowVilla();//平房别墅
        if(StringUtils.hasText(bungalowVilla)){
            PlotMessage plotMessage = JSON.parseObject(bungalowVilla, PlotMessage.class);
            List<BungalowVilla> bungalowVillas = plotMessage.getBungalowVillas();
            builder.append("平房/别墅区域,修改前:");
            for (BungalowVilla villa : bungalowVillas) {
                builder.append(villa.getArea());
                if(bungalowVillas.get(bungalowVillas.size()-1).equals(villa)){
                    builder.append(".");
                }else {
                    builder.append(",");
                }
            }
        }
        String bungalowVilla1 = sysPlot.getBungalowVilla();
        if(StringUtils.hasText(bungalowVilla1)){
            PlotMessage plotMessage = JSON.parseObject(bungalowVilla1, PlotMessage.class);
            List<BungalowVilla> bungalowVillas = plotMessage.getBungalowVillas();
            builder.append("平房/别墅区域,修改后:");
            for (BungalowVilla villa : bungalowVillas) {
                builder.append(villa.getArea());
                if(bungalowVillas.get(bungalowVillas.size()-1).equals(villa)){
                    builder.append(".");
                }else {
                    builder.append(",");
                }
            }
        }
        sysPlot1.setHouseholdsAvailable(sysPlot.getHouseholdsAvailable());
        sysPlot1.setPropertyName(sysPlot.getPropertyName());
        sysPlot1.setPropertyManager(sysPlot.getPropertyManager());
        sysPlot1.setPropertyPhone(sysPlot.getPropertyPhone());
        sysPlot1.setBuildingUnit(sysPlot.getBuildingUnit());//设置楼栋和单元
        sysPlot1.setBungalowVilla(sysPlot.getBungalowVilla());//设置平房别墅
        int rows = sysPlotDao.updateById(sysPlot1);
        if(Objects.isNull(sysPlot.getHouseholdsAvailable())){
            sysPlotDao.updateHouseholdsAvailable(sysPlot1.getId());
        }
        sysLog.setIncident("小区信息");
        sysLog.setType("修改");
        sysLog.setValue("业务日志");
        sysLog.setCreateTime(new Date());
        sysLog.setIp(GetIp.getIpAddr(request));
        User user2 = (User) ShiroUtils.getSubject().getPrincipal();
        sysLog.setOperator(user2.getUsername());
        sysLog.setContent("小区名称:"+sysPlot1.getName());
        sysLog.setDetails(builder.toString());
        if(rows > 0){
            sysLog.setResult("成功");
        }else{
            sysLog.setResult("失败");
        }
        int insert = sysLogDao.insert(sysLog);
        if (insert > 0) {
            log.info("日志记录成功");
        } else {
            log.error("日志记录失败");
        }

        return rows > 0 ? ResponseResult.builder().build() : ResponseResult.builder().message(ResultCode.SYSTEM_ERROR.getMessage()).code(ResultCode.SYSTEM_ERROR.getCode()).build();
    }
}
